package com.ruoyi.web.controller.commodity;

import com.ruoyi.commodity.service.IproductCatesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ProductCates;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-06
 */
@RestController
@RequestMapping("/commodity/classify")
public class productCatesController extends BaseController
{
    @Autowired
    private IproductCatesService productCatesService;

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:list')")
    @GetMapping("/list")
    public AjaxResult list(ProductCates productCates)
    {
        List<ProductCates> list = productCatesService.selectproductCatesList(productCates);
        return success(list);
    }

    /**
     * 查询产品列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:list')")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id" ,required = false) Long id)
    {
        List<ProductCates> productCates = productCatesService.selectproductCatesList(new ProductCates());
        productCates.removeIf(d -> d.getId().intValue()==id || ArrayUtils.contains(StringUtils.split(d.getAncestors(),","),id + ""));
        return success(productCates);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCates productCates)
    {
        List<ProductCates> list = productCatesService.selectproductCatesList(productCates);
        ExcelUtil<ProductCates> util = new ExcelUtil<ProductCates>(ProductCates.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productCatesService.selectproductCatesById(id));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductCates productCates)
    {
        if(!productCatesService.selectSole(productCates)){
            return error("新增产品"+productCates.getName()+"失败，产品名称已存在！");
        }
        return toAjax(productCatesService.insertproductCates(productCates));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProductCates productCates)
    {
        Long id = productCates.getId();
        if(!productCatesService.selectSole(productCates)){
            return error("修改产品"+productCates.getName()+"失败，产品名称已存在！");
        } else if (productCates.getParentId().equals(id)) {
            return error("修改产品"+productCates.getName()+"失败，上级产品不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE,productCates.getStatus()) && productCatesService.selectNormalChildrenById(id) > 0) {
            return error("该产品包含未停用的子部门");
        }
        return toAjax(productCatesService.updateproductCates(productCates));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('commodity:classify:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if(productCatesService.selectById(id)){
            return warn("存在下级产品，不允许删除");
        }
        return toAjax(productCatesService.deleteproductCatesById(id));
    }
}
